﻿//////////////////////////////////////////////////////////////////////////////////
//
// Project            : SilverlightAsyncSave
// Module             : SilverlightAsyncSave.Web
// Description        : FileWebService.
// 
// Repository         : $URL$
// Last changed by    : $LastChangedBy$
// Revision           : $LastChangedRevision$
// Last Changed       : $LastChangedDate$
// Author             : $Author$
//
// Id                 : $Id$
//
// Copyright          : (c) 2010 Torsten Bär
//
// Published under the MIT License. See license.txt or http://www.opensource.org/licenses/mit-license.php.
//
//////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SilverlightAsyncSave.Web
{
   /// <summary>
   /// FileService
   /// </summary>
   public class FileService : IFileService
   {
      /// <summary>
      /// Gets the available files.
      /// </summary>
      private static IEnumerable<string> Files
      {
         get
         {
            string codeBase = Uri.UnescapeDataString(new Uri(typeof(FileService).Assembly.CodeBase).AbsolutePath);
            string directory = Path.Combine(Path.GetDirectoryName(codeBase), "Files");

            return Directory.GetFiles(directory).ToList();
         }
      }

      /// <summary>
      /// Gets the files.
      /// </summary>
      /// <returns>The available files.</returns>
      public IList<string> GetFiles()
      {
         return Files.Select(Path.GetFileName).ToList();
      }

      /// <summary>
      /// Gets the content of the file.
      /// </summary>
      /// <param name="fileName">Name of the file.</param>
      /// <returns>The content.</returns>
      public string GetFileContent(string fileName)
      {
         string file = Files.FirstOrDefault(f => Path.GetFileName(f) == fileName);

         if (File.Exists(file))
            return File.ReadAllText(file);
         return null;
      }
   }
}
